package com.woniu.his.result;

import java.util.List;
import java.util.Set;

/**
 * @version: java version 1.8
 * @Author: Mr Ni
 * @description:
 * @date: 2022-11-03 12:01
 */
public class Result<T> {
    //    响应编码
    private Integer code;
    //    响应消息
    private String msg;
    //    响应数据
    private T data;

    public static Result<?> getInstance() {
        Result<?> re = new Result<>();
        re.setCode(CommonResultCode.SUCCESS.getCode());
        re.setMsg("处理成功");
        return re;
    }

    public static Result<?> getInstance(ResultCode resultCode) {
        Result<?> re = new Result<>();
        re.setCode(resultCode.getCode());
        re.setMsg(resultCode.getMessage());
        return re;
    }

    public static <S> Result<S> getInstance(Class<S>type) {
        Result<S> re = new Result<S>();
        re.setCode(CommonResultCode.SUCCESS.getCode());
        re.setMsg("处理成功");
        return re;
    }

    public static <S> Result<S> getInstance(Class<S>type, ResultCode resultCode) {
        Result<S> re = new Result<S>();
        re.setCode(resultCode.getCode());
        re.setMsg(resultCode.getMessage());
        return re;
    }

    public static <S> Result<List<S>> getInstanceList(Class<S>type) {
        Result<List<S>> re = new Result<List<S>>();
        re.setCode(CommonResultCode.SUCCESS.getCode());
        re.setMsg(CommonResultCode.SUCCESS.getMessage());
        return re;
    }

    public static <S> Result<Set<S>> getInstanceSet(Class<S>type) {
        Result<Set<S>> re =  new Result<Set<S>>();
        re.setCode(CommonResultCode.SUCCESS.getCode());
        re.setMsg("处理成功");
        return re;
    }

    public boolean isSuccess() {

        return code == CommonResultCode.SUCCESS.getCode();
    }

    public Integer getCode() {
        return code;
    }

    public Result<T> setCode(Integer code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public Result<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public T getData() {
        return data;
    }

    public Result<T> setData(T data) {
        this.data = data;
        return this;
    }

    @Override
    public String toString() {
        return "Result{" +
                "code=" + code +
                ",msg=" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}
